package com.jxc.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;
import com.jxc.model.vo.SessionInfo;
import com.jxc.model.vo.VResource;
import com.jxc.service.ResourceServiceI;
import com.jxc.util.ResourceUtil;

/**
 * 基础ACTION,其他ACTION继承此ACTION来获得writeJson和ActionSupport的功能
 * 
 * @author 孙宇
 * 
 */
@ParentPackage("basePackage")
@Namespace("/")
public class BaseAction {
	private static final Logger logger = Logger.getLogger(BaseAction.class);
	@Autowired
	private ResourceServiceI resourceServiceI;
	private VResource vResource;
	private String auths;

	/**
	 * 将对象转换成JSON字符串，并响应回前台
	 * 
	 * @param object
	 * @throws IOException
	 */
	public void writeJson(Object object) {
		try {
			// SerializeConfig serializeConfig = new SerializeConfig();
			// serializeConfig.setAsmEnable(false);
			// String json = JSON.toJSONString(object);
			// String json = JSON.toJSONString(object, serializeConfig,
			// SerializerFeature.PrettyFormat);
			String json = JSON.toJSONStringWithDateFormat(object,
					"yyyy-MM-dd HH:mm:ss");
			// String json = JSON.toJSONStringWithDateFormat(object,
			// "yyyy-MM-dd HH:mm:ss", SerializerFeature.PrettyFormat);
			ServletActionContext.getResponse().setContentType(
					"text/html;charset=utf-8");
			ServletActionContext.getResponse().getWriter().write(json);
			ServletActionContext.getResponse().getWriter().flush();
			ServletActionContext.getResponse().getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	protected HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	protected HttpSession getSession() {
		return ServletActionContext.getRequest().getSession();
	}

	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	protected String getIpAddr() {
		String ip = getRequest().getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = getRequest().getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = getRequest().getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = getRequest().getRemoteAddr();
		}
		return ip;
	}

	protected void getSubAuths(String psign) {
		SessionInfo sessionInfo = (SessionInfo) ServletActionContext
				.getRequest().getSession().getAttribute(
						ResourceUtil.getSessionInfoName());
		if (sessionInfo.getLoginName().equals("admin")) {
			auths = "all";
		} else {
			vResource = new VResource();
			vResource.setRid(Long.parseLong(sessionInfo.getRoleIds()));
			vResource.setSign(psign);
			List<VResource> list = resourceServiceI.getSubResources(vResource);
			auths = "";
			if (list != null) {
				for (VResource vResource : list) {
					auths += vResource.getSign() + ",";
				}
			}
		}

	}

	public VResource getvResource() {
		return vResource;
	}

	public void setvResource(VResource vResource) {
		this.vResource = vResource;
	}

	public String getAuths() {
		return auths;
	}

	public void setAuths(String auths) {
		this.auths = auths;
	}
}
